'use strict';

const Controller = require('egg').Controller;
const MockData = require('../mock/index')
const data = require('../mock/mock.json')

const py = require('js-pinyin')
const Mock = require('../mock/area')

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  async mock() {
    const { ctx, app } = this;
    await MockData.map((item) => {
      app.mysql.query(`insert into comelist(title,name,img,day,city,time,price,money) values("${item.title}","${item.name}","${item.img}","${item.day}","${item.city}","${item.time}","${item.price}","${item.money}")`)
    })
    ctx.body = {
      code: 200,
      msg: "成功",
      MockData
    }
  }

  async mockdata() {
    const { ctx, app } = this;
    await Mock.map((item) => {
      app.mysql.query(`insert into arealist(img,type,title,price,citys,screen) values("${item.img}","${item.type}","${item.title}","${item.price}","${item.citys}","${item.screen}")`)
    })
    ctx.body = {
      code: 200,
      msg: "成功",
      Mock
    }
  }

  


  async list() {
    const { ctx, app } = this;
    let data = await app.mysql.select('arealist')
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  async serect() {
    const { ctx, app } = this;
    const { value } = ctx.request.body;
    const data = await app.mysql.query(`select * from comelist where title like "%${value}%"`);
    ctx.body = {
      code: 200,
      msg: '成功',
      data
    }
  }

  async getlist() {
    const { ctx, app } = this
    let data = await app.mysql.select('state')
    ctx.body = {
      code: 200,
      msg: '成功',
      data
    }
  }

  async det() {
    const { ctx } = this;
    let list = data.filter(v => v.level > 1 && v.name !== '市辖区')
    //console.log(py.getFullChars(list[0].name));
    let cityObj = {}, //保存城市的对象
      sortObj = {}
    list.map(v => {
      //获取当前数据的name的拼音的首字母
      let firstWord = py.getFullChars(v.name).slice(0, 1)

      if (cityObj[firstWord]) {
        cityObj[firstWord].push(v)
      } else {
        cityObj[firstWord] = [v]
      }
    })
    //Object.keys: 返回由对象的键组成的新数组
    let wordList = Object.keys(cityObj).sort();
    wordList.map(v => {
      sortObj[v] = cityObj[v]

    })

    ctx.body = ({
      code: 1,
      data: sortObj
    })
  }

  async detail() {
    const { ctx, app } = this;
    let data = await app.mysql.get('state', { id: ctx.request.body.id })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  async area() {
    const { ctx, app } = this;
    const { select } = ctx.request.body;
    const data = await app.mysql.select('state', { where: { cityone: select } })
    if (data) {
      ctx.body = {
        code: "000",
        data
      }
    } else {
      ctx.body = {
        code: "001",
      }
    }
  }

  async area2() {
    const { ctx, app } = this;
    const { select2 } = ctx.request.body;
    const data = await app.mysql.select('state', { where: { housescreen: select2 } })
    if (data) {
      ctx.body = {
        code: '000',
        data
      }
    } else {
      ctx.body = {
        code: '001'
      }
    }
  }

  async area3() {
    const { ctx, app } = this;
    const { select3 } = ctx.request.body;
    const data = await app.mysql.select('state', { where: { senscs: select3 } })
    if (data) {
      ctx.body = {
        code: '000',
        data
      }
    } else {
      ctx.body = {
        code: '001'
      }
    }
  }
}

module.exports = HomeController;